package com.poetry.vup.works.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.poetry.vup.common.entity.works.VupExhibitionDO;
import com.poetry.vup.common.exception.KabobExceptionUtil;
import com.poetry.vup.common.result.BaseResult;
import com.poetry.vup.common.result.ResultCode;
import com.poetry.vup.common.result.page.BasePage;
import com.poetry.vup.common.result.page.PageParam;
import com.poetry.vup.common.util.JacksonUtil;
import com.poetry.vup.common.util.OSSClientUtil;
import com.poetry.vup.common.util.PageUtils;
import com.poetry.vup.user.domain.VupUserInfoDTO;
import com.poetry.vup.user.domain.VupUserInfoVO;
import com.poetry.vup.works.domain.VupDetialVO;
import com.poetry.vup.works.domain.VupExhibitionVO;
import com.poetry.vup.works.mapper.VupExhibitionMapper;
import com.poetry.vup.works.service.VupExhibitionService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
@Slf4j
public class VupExhibitionServiceImpl implements VupExhibitionService {

    @Autowired
    private VupExhibitionMapper vupExhibitionMapper;

    @Autowired
    private OSSClientUtil ossClientUtil;

    /**
     * 添加
     * @param vupExhibitionDO
     * @return
     */
    @Override
    public Long addVup(VupExhibitionDO vupExhibitionDO) {
        int insert = vupExhibitionMapper.insert(vupExhibitionDO);
        return vupExhibitionDO.getId();
    }

    /**
     * 查看所有vup
     * @return
     * @param pageSize
     * @param pageNum
     */
    @Override
    public BasePage<VupExhibitionVO> findAllVup(int pageSize, int pageNum) {
        List<VupExhibitionVO> list = new ArrayList<>();
        BasePage<VupExhibitionVO> page = new BasePage<>();

        int offset = (pageNum-1) * pageSize;//计算偏移量
        if(offset < 0){
            offset = 1;
        }

        Long count = null;
        list = vupExhibitionMapper.findAllVup(offset,pageSize);
        count = vupExhibitionMapper.getCount();
        if(list != null && list.size() != 0){
            return PageUtils.buildBasePage(list,new PageParam(pageNum,pageSize),count);
        }else {
            page.setList(list);
        }

        return page;
    }

    /**
     * 查看vup详情页
     * @param id
     * @return
     */
    @Override
    public VupDetialVO findDetail(Long id) {
        VupExhibitionDO vupExhibitionDO = vupExhibitionMapper.selectById(id);
        return JacksonUtil.objectToObject(vupExhibitionDO,VupDetialVO.class);
    }

    /**
     * 上传图片
     * @param file
     * @param id
     * @return
     */
    @Override
    public BaseResult uploadVupImage(MultipartFile file, Long id) {
        try {
            if(file != null){
                String str = ossClientUtil.uploadImg2Oss(file);
                String url = ossClientUtil.getUrl(str);
                VupExhibitionDO vupExhibitionDO = vupExhibitionMapper.selectById(id);
                vupExhibitionDO.setVupCover(url);
                int i = vupExhibitionMapper.updateById(vupExhibitionDO);
                return BaseResult.success(i);
            }
        }catch (Exception e){
            KabobExceptionUtil.throwError(ResultCode.FAILED,"操作失败",e);
        }
        return null;
    }

    @Override
    public List<VupExhibitionVO> findList() {
        return vupExhibitionMapper.findList();
    }

    /**
     * 添加
     * @param midId
     * @return
     */
    @Override
    public Long addVup(String midId) {
        String url = "https://api.bilibili.com/x/space/acc/info?mid="+midId;
        //String url = "https://api.bilibili.com/x/space/acc/info?mid=1368789598";

        try {
            HttpResponse httpResponse = HttpRequest.get(url).execute();
            String body = httpResponse.body();

            JSONObject jsonObject = JSON.parseObject(body);
            JSONObject jsonData = (JSONObject) jsonObject.get("data");
            VupExhibitionDO vupExhibitionDO = new VupExhibitionDO();
            vupExhibitionDO.setMidId(Long.valueOf(midId));
            //获取个人介绍
            String sign = (String) jsonData.get("sign");
            vupExhibitionDO.setVupSign(sign);
            //获取名称
            String name = (String) jsonData.get("name");
            vupExhibitionDO.setVupName(name);
            //获取头像
            String face = ossClientUtil.getUrl(ossClientUtil.uploadImg2Oss((String) jsonData.get("face")));
            vupExhibitionDO.setVupFace(face);
            //String replace = face.replace("http", "https");
            //获取封面图和图标
            JSONObject json = (JSONObject) jsonData.get("live_room");

            if(json!=null){
                String cover = ossClientUtil.getUrl(ossClientUtil.uploadImg2Oss((String) json.get("cover")));
                if(StringUtils.isNotBlank(cover)){
                    vupExhibitionDO.setVupCover(cover);
                }
                int insert = vupExhibitionMapper.insert(vupExhibitionDO);
                if(insert>=1){
                    return Long.valueOf(insert);
                }
            }

        }catch (Exception e){
            KabobExceptionUtil.throwError(ResultCode.FAILED,"操作失败",e);
        }
        return 0L;
    }

    /**
     * 搜索首页vupName
     * @param name
     * @return
     */
    @Override
    public List<VupExhibitionVO> findVupName(String name) {
        List<VupExhibitionDO> vupExhibitionDOS = vupExhibitionMapper.findVupName(name);
        return JacksonUtil.listToListObject(vupExhibitionDOS,VupExhibitionVO.class);
    }

    /**
     * 查找banner页
     */
    @Override
    public void findBanner() {

        vupExhibitionMapper.findBanner();

    }
}
